﻿Imports NVelocity
Imports NVelocity.App
Imports NVelocity.Context

Imports System.Collections.Generic
Imports System.IO

Imports System.Data
Imports System.Data.SqlClient

Partial Class WebForm_SearchTraining
    Inherits My.MyUserControl

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            VolunteerQueryControl1.IsVolunteerLeader = IIf(Me.CanAdmin = False And Me.CanQuery = True, True, False)
            VolunteerQueryControl1.InitData()
            Me.BindYear()
            Me.BindddlGroupId()

        End If
    End Sub

    Protected Sub ddlYear_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlYear.SelectedIndexChanged
        Me.BindddlGroupId()
    End Sub

    Protected Sub ddlGroupType_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlGroupType.SelectedIndexChanged
        Me.BindddlGroupId()
    End Sub

    Protected Sub btnQuery_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnQuery.Click

        'ObjectDataSource1.SelectParameters.Item("Region").DefaultValue = VolunteerQueryControl1.Region
        'ObjectDataSource1.SelectParameters.Item("Region2").DefaultValue = VolunteerQueryControl1.Region2
        ObjectDataSource1.SelectParameters.Item("VOrganizationID").DefaultValue = VolunteerQueryControl1.VOrganizationID
        ObjectDataSource1.SelectParameters.Item("VolunteerId").DefaultValue = VolunteerQueryControl1.VolunteerId
        ObjectDataSource1.SelectParameters.Item("VolunteerName").DefaultValue = VolunteerQueryControl1.VolunteerName
        ObjectDataSource1.SelectParameters.Item("TEL").DefaultValue = VolunteerQueryControl1.VolunteerTEL
        ObjectDataSource1.SelectParameters.Item("RegStatus").DefaultValue = ddlRegStatus.SelectedValue
        ObjectDataSource1.SelectParameters.Item("CheckStatus").DefaultValue = ddlCheckStatus.SelectedValue
        ObjectDataSource1.SelectParameters.Item("GroupID").DefaultValue = ddlUOrganizationID.SelectedValue
        ObjectDataSource1.SelectParameters("Annual").DefaultValue = Me.ddlYear.SelectedValue
        ObjectDataSource1.SelectParameters("UOrganizationID").DefaultValue = VolunteerQueryControl1.OrganizationID
        GridView1.DataSourceID = ObjectDataSource1.ID
        GridView1.DataBind()

    End Sub

#Region "DataBinding-將值分散"

    Protected Sub cblCheckStatus_DataBinding(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim cblCheckStatus As CheckBoxList = sender
        Dim gvr As GridViewRow = cblCheckStatus.NamingContainer

        Dim CheckStatus As Integer = DataBinder.Eval(gvr.DataItem, "CheckStatus").ToString()

        If CheckStatus = 1 Then
            cblCheckStatus.Items(0).Selected = True
            cblCheckStatus.Items(1).Selected = False
        ElseIf CheckStatus = 2 Then
            cblCheckStatus.Items(0).Selected = False
            cblCheckStatus.Items(1).Selected = True
        ElseIf CheckStatus = 3 Then
            cblCheckStatus.Items(0).Selected = True
            cblCheckStatus.Items(1).Selected = True
        Else
            cblCheckStatus.Items(0).Selected = False
            cblCheckStatus.Items(1).Selected = False
        End If

    End Sub

#End Region

    Private Sub BindddlGroupId()

        Dim dat As New clsData
        Dim sql As New MyStringBuilder

        Dim dt As Data.DataTable

        '所有的場次
        sql.AppendFormat("SELECT A.Id, ")
        sql.AppendFormat(" right('0'+CONVERT(varchar,Year(A.StartDate)-1911),3) + '/' +  right('0'+CONVERT(varchar,Month(A.StartDate)),2) + '/' +  right('0'+CONVERT(varchar,Day(A.StartDate)),2) +'  ' + A.GroupName as GroupName, ")
        sql.AppendFormat(" A.UOrganizationID, A.StartDate ")
        sql.AppendFormat("FROM TrainingCourseGroup A ")
        sql.AppendFormat("inner join CodeMapping B on A.GroupType=B.Value and B.TableName ='TrainingCourseGroup' AND B.ColumnName='GroupType' ")
        sql.AppendFormat(" Where 1=1 ")

        If Me.ddlYear.SelectedValue <> "" Then
            sql.AppendFormat(" And Year(A.StartDate) = '{0}' ", Me.ddlYear.SelectedValue)
        End If

        If Me.ddlGroupType.SelectedValue <> "" Then
            sql.AppendFormat(" And A.GroupType = '{0}' ", Me.ddlGroupType.SelectedValue)
        End If

        sql.AppendFormat("order by StartDate ")

        dt = dat.GetData(sql)

        ddlUOrganizationID.DataTextField = "GroupName"
        ddlUOrganizationID.DataValueField = "Id"

        ddlUOrganizationID.DataSource = dt
        ddlUOrganizationID.DataBind()

    End Sub

    Private Sub BindYear()

        Dim item As ListItem
        Dim i As Integer = DateTime.Now.Year

        item = New ListItem

        item.Text = "--請選擇--"
        item.Value = ""

        Me.ddlYear.Items.Add(item)


        While i > DateTime.Now.Year - 10

            item = New ListItem
            item.Text = Convert.ToString(i - 1911)
            item.Value = i.ToString()
            Me.ddlYear.Items.Add(item)
            i -= 1

        End While

    End Sub

    Public Function ChangeRegStatus(ByVal strRegStatus As String) As String

        If strRegStatus = "Y" Then
            Return "錄取"
        ElseIf strRegStatus = "B" Then
            Return "備取"
        ElseIf strRegStatus = "N" Then
            Return "取消報名"
        Else
            Return ""
        End If

    End Function

    Public Function ChangeCheckStatus(ByVal strCheckStatus As String) As String

        If strCheckStatus = "Y" Then
            Return "有報到"
        ElseIf strCheckStatus = "T" Then
            Return "臨時報到"
        ElseIf strCheckStatus = "N" Then
            Return "未報到"
        Else
            Return ""
        End If

    End Function
  

    Protected Sub ddlUOrganizationID_DataBound(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlUOrganizationID.DataBound

        Dim ddlUOrganizationID As DropDownList = TryCast(sender, DropDownList)

        If ddlUOrganizationID.Items.Count > 1 Then

            ddlUOrganizationID.Items.Insert(0, New ListItem(Me.GetLocalResourceObject("PleaseSelect"), String.Empty))

        End If

    End Sub

    Protected Sub ddlRegStatus_DataBound(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlRegStatus.DataBound
        If ddlRegStatus.Items.Count > 1 Then

            ddlRegStatus.Items.Insert(0, New ListItem(Me.GetLocalResourceObject("PleaseSelect"), String.Empty))

        End If
    End Sub
 
    Protected Sub ddlCheckStatus_DataBound(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlCheckStatus.DataBound
        If ddlCheckStatus.Items.Count > 1 Then

            ddlCheckStatus.Items.Insert(0, New ListItem(Me.GetLocalResourceObject("PleaseSelect"), String.Empty))

        End If
    End Sub

    Protected Sub btnExport_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnExport.Click

        Dim ResultDataTable As DataTable

        Dim bbl As New BLL_ManageTrainingRegister_SearchTraining
        Dim vList As New List(Of MyRowObjet)
        Dim v As MyRowObjet

        ResultDataTable = bbl.BLL_Select(Me.VolunteerQueryControl1.OrganizationID, Me.VolunteerQueryControl1.VOrganizationID, Me.VolunteerQueryControl1.VolunteerId, Me.VolunteerQueryControl1.VolunteerName, Me.VolunteerQueryControl1.VolunteerTEL, "'Y','B'", "3", Me.ddlUOrganizationID.SelectedValue, Me.ddlYear.SelectedValue)

        Dim theCount As Integer = ResultDataTable.Rows.Count

        If ResultDataTable IsNot Nothing AndAlso ResultDataTable.Rows.Count > 0 Then

            For i As Integer = 0 To theCount - 1

                v = New MyRowObjet

                v.CourseName = ResultDataTable.Rows(i)("GroupName")
                v.AllHour = ResultDataTable.Rows(i)("AllHour")
                v.StartDate = Convert.ToDateTime(ResultDataTable.Rows(i)("StartDate")).ToString("yyyy.MM.dd") 'IIf(String.IsNullOrEmpty(ResultDataTable.Rows(i)("WorkStartDate")), String.Empty, Convert.ToDateTime(ResultDataTable.Rows(i)("WorkStartDate")))

                v.VolunteerID = Left(ResultDataTable.Rows(i)("VolunteerID").ToString(), 3) & New String("X", 3) & Right(ResultDataTable.Rows(i)("VolunteerID").ToString(), 4)
                v.VolunteerName = ResultDataTable.Rows(i)("VolunteerName")
                vList.Add(v)

            Next

        End If

        'Print
        Dim fileName As String = "TrainingCourseList.xls"
        Response.AppendHeader("Content-Disposition", "attachment;filename=" + fileName)
        Response.ContentType = "application/vnd.ms-excel"

        Dim velocity As New VelocityEngine
        Dim props As New Commons.Collections.ExtendedProperties

        'fix bug
        props.AddProperty("file.resource.loader.path", Server.MapPath("~\App_Data"))
        props.AddProperty("input.encoding", "utf-8")
        props.AddProperty("output.encoding", "utf-8")
        velocity.Init(props)

        Dim template As Template

        Dim context As New VelocityContext
        context.Put("theCount", theCount + 1)
        context.Put("theList", vList)
        context.Put("User", clsAuth.GetUserID())
        context.Put("DownloadDate", Today.Year.ToString() & "-" & Today.Month.ToString() & "-" & Today.Day.ToString() & "T" & Now.Hour.ToString() & ":" & Now.Minute.ToString() & ":" & Now.Second.ToString() & "Z")
        context.Put("IP", MyObj.getIP())

        template = velocity.GetTemplate("志工訓練條v2.xml")

        Dim writer As New IO.StringWriter
        template.Merge(context, writer)

        Response.Write(writer.GetStringBuilder().ToString())

        Response.End()

    End Sub

    Protected Sub btnExport_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnExport.PreRender
        ScriptManager.GetCurrent(Me.Page).RegisterPostBackControl(sender)
    End Sub


End Class

Public Class MyRowObjet

    Private _CourseName As String
    Private _AllHour As String
    Private _StartDate As String
    Private _VolunteerName As String
    Private _VolunteerID As String
  
    Private IsHeaderField As Boolean

    Public Property CourseName() As String
        Get
            Return Me._CourseName
        End Get
        Set(ByVal value As String)
            Me._CourseName = value
        End Set
    End Property

    Public Property AllHour() As String
        Get
            Return Me._AllHour
        End Get
        Set(ByVal value As String)
            Me._AllHour = value
        End Set
    End Property

    Public Property StartDate() As String
        Get
            Return Me._StartDate
        End Get
        Set(ByVal value As String)
            Me._StartDate = value
        End Set
    End Property

    Public Property VolunteerName() As String
        Get
            Return Me._VolunteerName
        End Get
        Set(ByVal value As String)
            Me._VolunteerName = value
        End Set
    End Property
    Public Property VolunteerID() As String
        Get
            Return Me._VolunteerID
        End Get
        Set(ByVal value As String)
            Me._VolunteerID = value
        End Set
    End Property
 


End Class

